package com.cheng.operation.impl;

import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.DateUtil;
import com.cheng.common.utils.IdUtil;
import com.cheng.data.entity.DepartmentEntity;
import com.cheng.data.vo.DepartmentVO;
import com.cheng.data.vo.ReqPageInfoVO;
import com.cheng.data.vo.RespPageInfoVO;
import com.cheng.operation.DepartmentOperation;
import com.cheng.operation.GvmtTypeInfOperation;
import com.cheng.service.DepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.functors.ExceptionClosure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: ChengJW
 * @date: 2021/11/10 8:50
 * @version: 1.0
 * @description: 有关 department 的逻辑操作
 */
@Service
public class DepartmentOperationImpl implements DepartmentOperation {

    private final static Logger log = LoggerFactory.getLogger(DepartmentOperationImpl.class);

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private GvmtTypeInfOperation gvmtTypeInfOperation;

    /**
     * @author: ChengJW
     * @params: []
     * @return: com.cheng.data.vo.RespPageInfoVO
     * @date: 2021/11/11
     * @description: 部门信息列表查询，并返回 web 端，需要统一的格式封装
     */
    @Override
    public RespPageInfoVO selectDepartmentInfoList(ReqPageInfoVO vo) throws CommonException {

        DepartmentEntity entity = new DepartmentEntity();
        if (null != vo){
            //部门编号
            entity.setDtTypeCode(String.valueOf(vo.getQryReqFir()));
            //部门名称
            entity.setDtTypeNm(String.valueOf(vo.getQryReqSeco()));
            //所属机关代码
            entity.setDtUpsTypeCode(vo.getUserCode());
        }
        PageInfo<DepartmentEntity> pageInfo = PageHelper.startPage(vo.getPageNum(),vo.getPageSize()).doSelectPageInfo(() -> departmentService.selectDepartmentInfo(entity));

        List<DepartmentVO> list = new ArrayList<>();
        if (null != pageInfo.getList() && pageInfo.getList().size() > 0){
            for (DepartmentEntity e : pageInfo.getList()){
                DepartmentVO departmentVO = new DepartmentVO();
                departmentVO.setDtId(e.getDtId());
                departmentVO.setDtTypeCode(e.getDtTypeCode());
                departmentVO.setDtUpsTypeCode(e.getDtUpsTypeCode());
                //根据所属管辖机关代码，查询管辖机关名称
                departmentVO.setDtUpsTypeNm(gvmtTypeInfOperation.queryGvmtTypeInfNmByCode(e.getDtUpsTypeCode()));
                //拼接部门名称
                departmentVO.setDtTypeNm(e.getDtTypeNm());
                //创建时间戳
                departmentVO.setStDt(DateUtil.dateToStr(e.getStDt(),"yyyy-MM-dd"));
                list.add(departmentVO);
            }
        }
        RespPageInfoVO pageInfoVO = new RespPageInfoVO();
        pageInfoVO.setPageCount((int)pageInfo.getTotal());
        pageInfoVO.setPageSize(pageInfo.getPageSize());
        pageInfoVO.setPageNum(pageInfo.getPageNum());
        pageInfoVO.setList(list);
        return pageInfoVO;
    }

    /**
    * @author: ChengJW
    * @params: [departmentVO]
    * @return: com.cheng.data.vo.DepartmentVO
    * @date: 2021/11/11
    * @description: 当前登录用户能够查询到的部门编号最大的一条部门信息，将部门编号+1，作为新增部门信息的部门编号,并返回给前端
    */
    @Override
    public DepartmentVO initAddDepartmentInfo(DepartmentVO departmentVO) throws CommonException {

        DepartmentEntity entity = new DepartmentEntity();
        if (null != departmentVO){
            BeanUtils.copyProperties(departmentVO,entity);
            //将用户的单位代码存入部门信息的所属机关代码
            entity.setDtUpsTypeCode(departmentVO.getUserCode());
        }
        List<DepartmentEntity> list = departmentService.selectDepartmentInfo(entity);
        DepartmentVO vo = new DepartmentVO();
        if (null != list && list.size() > 0){
            DepartmentEntity departmentEntity = list.get(0);
            vo.setDtTypeCode(departmentEntity.getDtTypeCode());
            vo.setDtUpsTypeCode(departmentEntity.getDtUpsTypeCode());
            vo.setDtUpsTypeNm(gvmtTypeInfOperation.queryGvmtTypeInfNmByCode(departmentEntity.getDtUpsTypeCode()));
        }
        return vo;
    }

    /**
     * @author: ChengJW
     * @params: []
     * @return: int
     * @date: 2021/11/11
     * @description: 新增部门信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertDepartmentInfo(DepartmentVO departmentVO) throws CommonException {

        if (null == departmentVO){
            log.error("部门信息为空");
            throw new CommonException("CJW202111010041","DepartmentOperationImpl","insertDepartmentInfo");
        }
        DepartmentEntity entity = new DepartmentEntity();
        //新增之前进行查询,是否已经存在相同的部门信息,不能存在相同的部门名称
        entity.setDtTypeNm(departmentVO.getDtTypeNm());
        entity.setDtUpsTypeCode(departmentVO.getDtUpsTypeCode());
        List<DepartmentEntity> list = departmentService.selectDepartmentInfo(entity);
        if (null != list && list.size() > 0){
            return -1;
        }
        BeanUtils.copyProperties(departmentVO,entity);
        entity.setDtId(IdUtil.getDepId());
        entity.setStDt(new Date());
        entity.setUtDt(new Date());
        return departmentService.insertDepartmentInfo(entity);
    }

    /**
    * @author: ChengJW
    * @params: [departmentVO]
    * @return: int
    * @date: 2021/11/12
    * @description: 修改部门信息
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateDeapartmentInfo(DepartmentVO departmentVO) throws CommonException {

        if (null == departmentVO){
            log.error("部门信息为空，无法进行后续操作");
            throw new CommonException("CJW202111010034","DepartmentOperationImpl","updateDeapartmentInfo");
        }
        DepartmentEntity entity = new DepartmentEntity();
        BeanUtils.copyProperties(departmentVO,entity);
        return departmentService.updateDepartmentInfo(entity);
    }

    /**
    * @author: ChengJW
    * @params: [departmentVO]
    * @return: int
    * @date: 2021/11/12
    * @description: 删除部门信息
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteDepartmentInfo(DepartmentVO departmentVO) throws CommonException {

        if(null == departmentVO){
            log.error("部门信息为空，无法进行后续操作");
            throw new CommonException("CJW202111010034","DepartmentOperationImpl","deleteDepartmentInfo");
        }
        DepartmentEntity entity = new DepartmentEntity();
        BeanUtils.copyProperties(departmentVO,entity);
        return departmentService.deleteDepartmentInfo(entity);
    }
}
